home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / Cookie / Source / StellaView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.1 KB  |  164 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "StellaView.h"
  5. #import <appkit/appkit.h>
  6. #import <dpsclient/dpsclient.h>
  7.  
  8. void RunStellaEntry(DPSTimedEntry te, double timeNow, void *data)
  9. {
  10.   /* we set data to self so we can call this method from the timed entry */
  11.     [(StellaView *)data display];
  12. }
  13.  
  14. @implementation StellaView
  15.  
  16. - showStar :(int)x :(int)y :(int)cycle
  17. {
  18.     NXImage *myImage = NULL;
  19.     NXPoint myPoint;
  20.  
  21.     switch(cycle)
  22.     {
  23.         case 0    :    myImage = [NXImage findImageNamed:"Stella1"];break;
  24.         case 1    :    myImage = [NXImage findImageNamed:"Stella2"];break;
  25.         case 2    :    myImage = [NXImage findImageNamed:"Stella3"];break;
  26.         case 3    :    myImage = [NXImage findImageNamed:"Stella4"];break;
  27.         case 4    :    myImage = [NXImage findImageNamed:"Stella5"];break;
  28.         case 5    :    myImage = [NXImage findImageNamed:"Stella6"];break;
  29.         case 6    :    myImage = [NXImage findImageNamed:"Stella7"];break;
  30.     }
  31.  
  32.     myPoint.x = x*STAR_WIDTH;
  33.     myPoint.y = y*STAR_HEIGHT;
  34.     [myImage composite:NX_COPY toPoint:&myPoint];
  35.  
  36.     return self;
  37. }
  38.  
  39. - deleteStar :(int)x :(int)y
  40. {
  41.     NXRect myRect;
  42.  
  43.     PSsetgray(0.0);
  44.  
  45.     myRect.origin.x = x*STAR_WIDTH;
  46.     myRect.origin.y = y*STAR_HEIGHT;
  47.     myRect.size.width = STAR_WIDTH;
  48.     myRect.size.height = STAR_HEIGHT;
  49.  
  50.     NXRectFill(&myRect);
  51.  
  52.     return self;
  53. }
  54.  
  55. - drawSelf:(const NXRect *)rects :(int)rectCount;
  56. {
  57.     int count;
  58.  
  59.     if(!flags.blankedscreen)
  60.     {
  61.         PSsetgray(0.0);
  62.         NXRectFill(&bounds);
  63.         flags.blankedscreen = TRUE;
  64.     }
  65.  
  66.     // Show all the stars
  67.     for(count = 0; count < NUMBER_STARS; count++)
  68.         [self showStar :stars[count].x :stars[count].y :stars[count].cycle];
  69.  
  70.     for(count = 0; count < NUMBER_STARS; count++)
  71.     {
  72.         if(!stars[count].forward)
  73.             stars[count].cycle--;
  74.         else
  75.             stars[count].cycle++;
  76.  
  77.         if(stars[count].cycle == stars[count].magnitude)
  78.             stars[count].forward = !stars[count].forward;
  79.  
  80.         if(stars[count].cycle == -1)
  81.         {
  82.             [self deleteStar :stars[count].x :stars[count].y];
  83.             [self makeMyStar:count];
  84.         }
  85.     }
  86.  
  87.     return self;
  88. }
  89.  
  90. - startAnimation
  91. {
  92.     myTimedEntry =
  93.         DPSAddTimedEntry((float)0.1,
  94.             &RunStellaEntry,self,NX_BASETHRESHOLD);
  95.  
  96.     return self;
  97. }
  98.  
  99. - endAnimation
  100. {
  101.     DPSRemoveTimedEntry(myTimedEntry);
  102.  
  103.     return self;
  104. }
  105.  
  106. - initFrame:(const NXRect *)frameRect
  107. {
  108.     int count;
  109.  
  110.     [super initFrame :frameRect];
  111.  
  112.     flags.blankedscreen = FALSE;
  113.  
  114.     width = frameRect->size.width/STAR_WIDTH;
  115.     height = frameRect->size.height/STAR_HEIGHT;
  116.  
  117.     for(count = 0; count < NUMBER_STARS; count++)
  118.         stars[count].magnitude = 0;
  119.  
  120.     for(count = 0; count < NUMBER_STARS; count++)
  121.     {
  122.         [self makeMyStar :count];
  123.         stars[count].cycle = random()%stars[count].magnitude;
  124.         stars[count].forward = random()%2;
  125.     }
  126.  
  127.     return self;
  128. }
  129.  
  130. - (BOOL)starExists :(int)x :(int)y
  131. {
  132.     int count;
  133.  
  134.     for(count = 0; count < NUMBER_STARS; count++)
  135.         if((stars[count].magnitude) && (stars[count].x == x) &&
  136.            (stars[count].y == y))
  137.             return TRUE;
  138.  
  139.     return FALSE;
  140. }
  141.  
  142. - makeMyStar :(int)number
  143. {
  144.     int x,y;
  145.  
  146.     do
  147.     {
  148.         x = random()%width;
  149.         y = random()%height;
  150.     }
  151.     while([self starExists :x :y]);
  152.  
  153.     stars[number].x = x;
  154.     stars[number].y = y;
  155.  
  156.     stars[number].cycle = 0;
  157.     stars[number].magnitude = random()%(MAX_MAGNITUDE-1)+1;
  158.     stars[number].forward = TRUE;
  159.  
  160.     return self;
  161. }
  162.  
  163. @end
  164.